package servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import action.Action;
import bean.XmlBean;
import form.ActionForm;
import util.FullForm;




public class ActionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		String path = this.getPath(request.getServletPath());//获取页面访问的path即访问的xxx.do
		Map<String,XmlBean> map = (Map)this.getServletContext().getAttribute("struts");//获取注解xmlbean map
		XmlBean xml = map.get(path);//通过页面访问的.do,获取对应的xmlbean
		String formClass = xml.getFormClass();//获取页面对应的form
		ActionForm form = FullForm.full(formClass,request);//将页面请求信息装载成一个form对象
		String actionType = xml.getActionClass();//获取一个action路径,名称
		Action action = null;
		String forwardUrl=null;
		try{
			Class clazz = Class.forName(actionType);//通过访问的action名称,反射获取该action
			action = (Action) clazz.newInstance();//实例化action
			forwardUrl = action.execute(request,response,form,xml.getActionForward());//action执行后返回信息
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("控制器错误");
		}
		System.out.println(forwardUrl);
		RequestDispatcher dis = request.getRequestDispatcher(forwardUrl);//获取跳转对象,参数为要跳转到的页面路径
		dis.forward(request, response);//进行页面跳转,action执行完毕
		
		

	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		this.doGet(request, response);	
	}
	private String getPath(String servletPath){
		return servletPath.split("\\.")[0];
	}

}







































